<?php

namespace App\Http\Controllers\Company;

use App\Http\Controllers\Core\HomeController as Home;
use App\Models\Category;
use App\Models\CompanyProfile;
use App\Models\Job;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class JobController extends Home
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @param int $id
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index($id = 0, Request $request)
    {
        //TODO 后期ajax 请求
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if($this->member->type != 1){
            return redirect('login');
        }
        $user_id = Session::get('member')->id;
        //判断是否
        $job = Job::where(['user_id' => $user_id])
            ->orderBy('display', 'ASC')->orderBy('refresh_time', 'DESC');
        if ($id) {
            $job = $job->where(['display' => $id]);
        }
        $job = $job->paginate(10);
        return view('company.job.index', ['id' => $id, 'job' => $job]);
    }

    /**
     *  新增岗位
     * @param int $id
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id = 0, Request $request)
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if($this->member->type != 1){
            return redirect('login');
        }
        $job = Job::where(['user_id' => Session::get('member')->id])->find((int)$id);

        $category = Category::select('id', 'pid', 'name')
            ->where(['alias' => '_trade'])
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();
        $book = Category::category_caches();

        if ($request->method() == 'POST') {
            //表单提交处理
            $member = Session::get('member');
            $user_id = isSet($member->id) ? $member->id : 0;
            if (!$user_id) {
                return json_encode(['data' => '', 'msg' => '请重新登录', 'status' => -1]);
            }
            //获取公司信息
            $company = CompanyProfile::where(['user_id' => $user_id])->get();
            if (!$company = $company->first()) {
                return json_encode(['data' => '', 'msg' => '信息有误，请重新登录', 'status' => -1]);
            }
            //判断职位类别
            $category_id = (int)$request->get('category');
            $category_arr = array_column($category, 'name', 'id');
            $category_cn = isSet($category_arr[$category_id]) ? $category_arr[$category_id] : "";
            if (empty($category_cn)) {
                return json_encode(['data' => '', 'msg' => '职位分类出错请重新刷新页面后重新尝试', 'status' => -1]);
            }
            //判断登陆

            $time = time();
            // 判断提交还是修改
            $id = (int)$request->get('id');
            if ($id == 0) {
                $job = new Job();
                $job->create_time = $time;
            } else {
                $job = Job::find($id);
                if (!$job->first()) {
                    return json_encode(['data' => '', 'msg' => '失败', 'status' => 0]);
                }
            }
            $job->jobs_name = $request->get('jobs_name');
            $job->user_id = $user_id;
            $job->company_id = $company->id;
            $job->company_cn = $company->name;
            $job->nature = $company->nature;
            $job->nature_cn = $company->nature_cn;
            $job->amount = (int)$request->get('number');
            $job->category = $category_id;
            $job->category_cn = $category_cn;
            $job->scale = $company->scale;
            $job->scale_cn = $company->scale_cn;
            $job->tag = $company->tag;
            $dictionary = Category::category_cache();
            $job->tag_cn = Category::getCategoryCns($dictionary, '_job_tag', $company->tag);
            $job->mobile = $request->get('mobile');
            $job->negotiable = (int)$request->get('negotiable');
            //默认只支持杭州工作
            $job->contents = $request->get('contents');
            $job->city = $request->get('city');
            $job->city_cn = $request->get('city_cn');
            $job->proper = $request->get('proper');
            $job->proper_cn = $request->get('proper_cn');
            $job->area = $request->get('area');
            $job->area_cn = $request->get('area_cn');

            $job->district_cn = $request->get('city_cn') . '-' . $request->get('proper_cn') . '-' . $request->get('area_cn');
            $job->refresh_time = $time;

            $job->city = $request->get('city');
            if (!$job->negotiable) {
                $job->min_wage = $request->get('min_wage');
                $job->max_wage = $request->get('max_wage');
            }
            $job->education = $request->education;
            $job->experience = $request->experience;
            $job->amount = isSet($request->amount) ? $request->amount : 0;
            $job->sex = $request->sex;
            if ($job->save()) {
                return json_encode(['data' => ['url' => url('company/job')], 'msg' => '成功', 'status' => 1]);
            } else {
                return json_encode(['data' => '', 'msg' => '失败', 'status' => 0]);
            }
        }
        $data = [
            'book' => $book,
            'category' => json_encode($this->list_to_tree($category)),
            'job' => $job
        ];
        return view('company.job.edit', $data);
    }

    /*删除*/
    public function delete($id = 0)
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if($this->member->type != 1){
            return redirect('login');
        }
        $job = Job::find($id);
        if ($job) {
            $job->display = 2;
            $job->save();
        }
        return redirect::back();
    }

    /*恢复*/
    public function regain($id = 0)
    {
        if (!$this->isLogin()) {
            return redirect('login');
        }
        if($this->member->type != 1){
            return redirect('login');
        }
        $job = Job::find($id);
        if ($job) {
            $job->display = 1;
            $job->refresh_time = time();
            $job->save();
        }
        return redirect::back();
    }

    public function interview()
    {
        if($this->member->type != 1){
            return redirect('login');
        }
        return view('company.job.interview');
    }

    function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
    {
        // 创建Tree
        $tree = array();
        if (is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[$pid];
                if ($root == $parentId) {
                    $tree[$data['id']] =& $list[$key];
                } else {
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][$data['id']] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }
}